package com.qf.servlet;

import com.qf.convert.MyConvert;
import com.qf.pojo.Admin;
import com.qf.pojo.PageBean;
import com.qf.pojo.User;
import com.qf.service.AdminService;
import com.qf.service.impl.AdminServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

/**
 * 针对后台管理员的模块
 */
@WebServlet("/admin")
public class AdminServlet extends BaseServlet {


    /**
     * 管理员的登录功能
     * @param request 请求对象
     * @param response 响应对象
     */
    public void admin_login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {


        //1)获取前台用户提交的参数
        String name = request.getParameter("name");
        String password = request.getParameter("password");

        // System.out.println(name+"---"+password) ;

        //封装管理实体
        Admin admin = new Admin() ;
        admin.setName(name) ;
        admin.setPassword(password) ;

        //调用业务层接口
        AdminService adminService = new AdminServiceImpl() ;
        Admin loginAdmin = adminService.isLogin(admin) ;

        if(loginAdmin!=null){
            //true 成功

            //用户登录成功之后,需要将用户的信息 存储session中(存储服务器)
            //通过请求对象获取HttpSession, 将用户存储到服务器端
            // HttpSession getSession();
            HttpSession session = request.getSession();

            session.setAttribute("admin",loginAdmin);


            //请求转发 到admin_index.jsp
            request.getRequestDispatcher("/admin/admin_index/admin_index.jsp").forward(request,response);

        }else{
            //请求转发login.jsp
            request.setAttribute("msg","用户名或者密码输入错误") ;
            request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        }

    }

    /**
     * 管理员分页查询用户
     * @param request 请求对象
     * @param response 响应对象
     */
    public void findUserByPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        System.out.println("findUserByPage方法调用了...");
        //1)接收参数

        String curPage = request.getParameter("currentPage"); //接收当前页码
        String pSize = request.getParameter("pageSize");//接收每页显示的条数

        //判断
        if(curPage==null || "".equals(curPage)){
            //给默认值
            curPage = "1" ;
        }
        if(pSize== null || "".equals(pSize)){
            //给默认值
            pSize = "2" ;
        }

        //将curPage和pSize转换成 int类型
        int currentPage = Integer.parseInt(curPage) ;
        int pageSize = Integer.parseInt(pSize);

        //2)调用业务接口,获取分页实体 PageBean
        AdminService adminService = new AdminServiceImpl() ;
        PageBean<User> pb = adminService.findUserByPage(currentPage, pageSize);
        System.out.println(pb);
        //3)将PageBean对象存储在request域中,请求转发到admin/amdin_index/user_list.jsp
        request.setAttribute("pb",pb) ;
        request.getRequestDispatcher("/admin/admin_index/user_list.jsp").forward(request,response);
    }

    /**
     * 管理员根据用户uid查询用户
     * @param request
     * @param response
     */
    public void findUserByUid(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        // href="${pageContext.request.contextPath}/findUserByUid?uid=${u.uid}"
        //1)获取路径携带的uid参数
        String uid = request.getParameter("uid");
        // System.out.println(uid) ;
        //2)调用业务接口
        AdminService adminService = new AdminServiceImpl()  ;
        User user = adminService.getUser(uid) ;
        System.out.println(user) ;
        //将user对象存储在request域中
        request.setAttribute("user",user) ;
        //请求转发到 /admin/admin_index/update_user.jsp
        request.getRequestDispatcher("/admin/admin_index/update_user.jsp").forward(request,response);

    }

    /**
     * 管理员修改用户
     * @param request
     * @param response
     */
    public void updateUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {


        //接收参数: 单个获取参数,封装User对象
       /* String uid = request.getParameter("uid") ;//用户编号
        String username = request.getParameter("username");//昵称
        String password = request.getParameter("password");//密码
        String name = request.getParameter("name");//真实姓名
        String email = request.getParameter("email");//邮箱
        String myBirthday = request.getParameter("birthday");//出生日期

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
        Date birthday = null;
        try {
            birthday = sdf.parse(myBirthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        User user = new User() ;
        user.setUid(uid);
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setEmail(email);
        user.setBirthday(birthday);
        System.out.println(user);*/

        //方式2:使用common-beanutils.jar以及两个依赖包
        //获取当前表单提交的所有参数的Map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //创建User对象
        User user = new User() ;

        //使用common-beanutils.jar ----.有一个转换器工具类--->开发者可以自定义日期转换器
        ConvertUtils.register(new MyConvert(), Date.class); //注册转换器
        //参数1:Convert接口---> 自定义一个类实现接口,重写里面convert方法
        //参数2:转换成最终类型 的Class  (Date类型)

        //使用工具类
        try {
            //封装User对象了
            BeanUtils.populate(user,parameterMap) ;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        // System.out.println(user);
        //调用业务接口
        AdminService adminService = new AdminServiceImpl() ;
        adminService.updateUser(user) ;

        //请求转发到查询所有的用户的后台
        request.getRequestDispatcher("/admin?methodName=findUserByPage").forward(request,response);

    }

    /**
     * 管理员根据用户uid查询用户
     * @param request 请求对象
     * @param response 响应对象
     */
    public void delUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        //1)接收地址携带的参数
        String uid = request.getParameter("uid");
        //System.out.println(uid);

        //2)调用AdminService业务接口
        AdminService adminService = new AdminServiceImpl() ;
        adminService.delUser(uid) ;

        //请求转发到  "/findAllUser"
        request.getRequestDispatcher("/admin?methodName=findUserByPage").forward(request,response);
    }

}
